package com.gexiaobao.pigeon.ui.pigeoncatcher

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ActivityHistoryOrderDrippingBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterFinishOrder
import com.gexiaobao.pigeon.viewmodel.PigeonCatcherViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/7/2 16:18
 * @Describe    : 历史订单（接鸽员）
 */
class ActivityHistoryOrderDripping : BaseActivity<PigeonCatcherViewModel, ActivityHistoryOrderDrippingBinding>() {

    private var mOrgId: Int = 0
    private val mAdapter: AdapterFinishOrder by lazy { AdapterFinishOrder() }
    private lateinit var loadsir: LoadService<Any>

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = "历史订单"

        mOrgId = intent.getIntExtra("orgId", 0)

        //状态页配置
        loadsir = loadServiceInit(mDatabind.includeList.swipeRefreshLayout) {
            //点击重试时触发的操作
            onRefresh(true)
        }
        onRefresh(true)

        mDatabind.includeList.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(this, 10f)))
            it.initFooter {
                onRefresh(false)
            }
        }

        mDatabind.includeList.swipeRefreshLayout.init {
            onRefresh(true)
        }
        mAdapter.setOnItemClickListener { _, _, position ->
            startActivity<ActivityFinishDrippingOrder>("id" to mAdapter.data[position].id)
        }
    }

    private fun onRefresh(b: Boolean) {
        mViewModel.getOrderDidiListWithTransporter(b, mOrgId, 5, 0.0, 0.0)
    }

    override fun createObserver() {
        super.createObserver()
        /** 获取订单列表（接鸽员角色）  */
        mViewModel.orderListResult.observe(this) {
            loadListData(it, mAdapter, loadsir, mDatabind.includeList.swipeRecyclerView, mDatabind.includeList.swipeRefreshLayout)
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack) {
            when (it) {
                mDatabind.includeBar.ivBack -> {
                    finish()
                }
            }
        }
    }
}